package application.gui.adminWindow;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;

import application.CommunicationModule.Communication;
import application.LoginAndAdministrationModule.Admin;
import application.LoginAndAdministrationModule.PersonData;
import application.client.data.Client;

public class WorkerDetails extends JFrame {
	public WorkerDetails(AdminHomePage parent, Communication c, PersonData p, Admin a) {
		this.parent = parent;
		this.db = c;
		this.admin = a;
		this.personData = p;
		setSize(480, 320);
		setLayout(null);
		setResizable(false);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		initTable();
		initComponents();
	}

	private void initComponents() {
		buttonPanel = new JPanel(null);
		buttonPanel.setSize(480, 50);
		close = new JButton("Zamknij");
		buttonPanel.add(close);
		close.setBounds(300, 0, 100, 40);
		deleteWorker = new JButton("Usuń");
		buttonPanel.add(deleteWorker);
		deleteWorker.setBounds(0, 0, 150, 40);
		changePassword = new JButton("Zmień hasło");
		buttonPanel.add(changePassword);
		changePassword.setBounds(150, 0, 150, 40);
		add(buttonPanel, BorderLayout.SOUTH);
		buttonPanel.setBounds(0, 200, 480, 50);
		close.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				parent.setVisible(true);
				dispose();
			}
		});
		deleteWorker.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					if (admin.deleteUser(personData.getId())){
						parent.setVisible(true);
						parent.getShowWorkers().doClick();
						dispose();
					}
				} catch (SQLException e1) {
					
					e1.printStackTrace();
				} 
			}
		});
		changePassword.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String newPass = JOptionPane.showInputDialog(parent, "Podaj nowe hasło");
				try {
					if (admin.change_users_password(personData.getLogin(), newPass)){
						JOptionPane.showMessageDialog(parent, "Hasło zmienione", "OK", JOptionPane.INFORMATION_MESSAGE);
						}
				} catch (Exception e1) {
					
					e1.printStackTrace();
				}
			}
		});
	}

	public void initTable() {
		// TODO Auto-generated method stub
		LinkedList<String[]> list = new LinkedList<String[]>();
		Client recipien;
		PersonData sender;
		String[] row = { "ID pracownika", String.valueOf(personData.getId()) };
		list.add(row);
		row = new String[] { "Imię i nazwisko",
				personData.getName()+ " " + personData.getSurname() };
		list.add(row);
		row = new String[] { "Login", personData.getLogin()};
		list.add(row);
		row = new String[] {
				"Email",
				personData.geteMail()};
		list.add(row);
		row = new String[] { "Nr telefonu", personData.getephoneNumber()};
		list.add(row);
		row = new String[] { "Uprawnienia",
				String.valueOf(personData.getRights())};
		list.add(row);
		data = list.toArray(new String[2][list.size()]);

		table = new JTable(data, names);
		add(table);
		table.setBounds(0, 0, 480, 200);

	}

	private AdminHomePage parent;
	private PersonData personData;
	private JTable table;
	private String[] names = { "", "" };
	private String[][] data;
	private JButton close;
	private JButton deleteWorker;
	private JButton changePassword;
	private JPanel buttonPanel;
	private Admin admin;
	private Communication db;

}
